package pingan.com.imandtwo.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.beans.IsRegisterBean;
import pingan.com.imandtwo.beans.ValidateCodeBean;
import pingan.com.imandtwo.interfaces.Iview;
import pingan.com.imandtwo.presenter.HttpPresenter;
import pingan.com.imandtwo.utils.BaseActivity;

/**
 * Created by xsj on 2018/2/5.
 * 注册界面
 */
public class RegisterActivity extends BaseActivity implements Iview {

    @BindView(R.id.text_input_phone)
    EditText textInputPhone;
    @BindView(R.id.verification_number)
    EditText verificationNumber;
    @BindView(R.id.but_getyanzhen)
    Button butGetyanzhen;
    @BindView(R.id.input_password)
    EditText inputPassword;
    @BindView(R.id.input_confirm_password)
    EditText inputConfirmPassword;
    @BindView(R.id.real_name)
    EditText realName;
    @BindView(R.id.but_register)
    Button butRegister;
    @BindView(R.id.but)
    Button but;
    private Unbinder bind;
    private HttpPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bind = ButterKnife.bind(this);

        presenter = new HttpPresenter(this);
    }

    @OnClick({R.id.but_getyanzhen, R.id.but_register,R.id.but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //发送验证码
            case R.id.but_getyanzhen:
                String phone = textInputPhone.getText().toString();
                if (!TextUtils.isEmpty(phone)) {
                    if (isPhone(phone)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phoneNumber", phone);
                        map.put("type", "1");
                        presenter.getMap(map, "注册验证码");
                    } else {
                        Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RegisterActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;

            //查看手机是否可被注册
            case R.id.but:
                String phone1 = textInputPhone.getText().toString();
                if (!TextUtils.isEmpty(phone1)) {
                    if (isPhone(phone1)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phoneNumber", phone1);
                        map.put("type", "1");
                        presenter.getMap(map, "是否注册");
                    } else {
                        Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RegisterActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;

            //注册
            case R.id.but_register:
                String userphone = textInputPhone.getText().toString();
                String useryanzhen = verificationNumber.getText().toString();
                String userpsd = inputPassword.getText().toString();
                String userpsd2 = inputConfirmPassword.getText().toString();
                String username = realName.getText().toString();

                //判空
                if (isNull(userphone, useryanzhen, userpsd, userpsd2, username)) {
                    if (userpsd.equals(userpsd2)) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("phoneNumber", userphone);
                        map.put("password", userpsd);
                        map.put("password1", userpsd2);
                        map.put("realName", username);
                        map.put("verify", useryanzhen);
                        presenter.getMap(map, "注册");
                    } else {
                        Toast.makeText(RegisterActivity.this, "两次密码不一致，请检查，爱你么么哒", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }

    }

    @Override
    public void onSuccess(Object o, String tag) {

        if (tag.equals("注册验证码")) {
            ValidateCodeBean codeBean = (ValidateCodeBean) o;
            if (codeBean.getStatus().equals("0000")) {
                Toast.makeText(RegisterActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
            }
        }

        if (tag.equals("注册")) {
            ValidateCodeBean codeBean = (ValidateCodeBean) o;
            Toast.makeText(RegisterActivity.this, codeBean.getMessage().toString(), Toast.LENGTH_SHORT).show();
        }

        if (tag.equals("是否注册")) {
            IsRegisterBean isRegisterBean = (IsRegisterBean) o;
            String status = isRegisterBean.getStatus();
            if (status.equals("1001")) {
                Toast.makeText(RegisterActivity.this, "当前手机号已被注册", Toast.LENGTH_SHORT).show();
            }

            if(status.equals("0000")){
                Toast.makeText(RegisterActivity.this, "用户还未注册,账户可用", Toast.LENGTH_SHORT).show();
            }
        }

    }

    //输入框判空
    public Boolean isNull(String userphone, String useryanzhen, String userpsd, String userpsd2, String username) {
        if (TextUtils.isEmpty(userphone) || TextUtils.isEmpty(useryanzhen) || TextUtils.isEmpty(userpsd) || TextUtils.isEmpty(userpsd2) || TextUtils.isEmpty(username)) {
            Toast.makeText(RegisterActivity.this, "有输入框的值为空，请检查！！！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    //手机号判断
    public Boolean isPhone(String username) {
        //正则表达式
        String regex = "^1[3|4|5|7|8]\\d{9}";
        return Pattern.matches(regex, username);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
        presenter.disAttach();
    }
}
